/**
 * Project: TacticaPescaditos
 * File: TendGoal.java
 */
package org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.fsm.state;

import org.javahispano.javacup.modelo.Constantes;

import org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.GoalKeeper;
import org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.geometrics.Vector2D;
import org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.messages.Telegram;

/**
 * @author Carlos Alberto
 * class: TendGoal
 */
public class TendGoal implements State<GoalKeeper> {
	
	/**
	 * 
	 */
	private TendGoal() {
	}

	public static final State<GoalKeeper> instance = new TendGoal();

	public void enter(GoalKeeper keeper) {
		keeper.getSteering().seekOn();	
		keeper.getSteering().setTarget(keeper.getRearInterposeTarget());
	}

	public void execute(GoalKeeper keeper) {
		Vector2D rearInterposeTarget = keeper.getRearInterposeTarget();
		
		if (keeper.isAtTarget()) {
			keeper.getSteering().seekOff();
			keeper.setVelocity(new Vector2D());
			keeper.getSteering().setTarget(rearInterposeTarget);
		} else {
			keeper.getSteering().seekOn();
			
		}

		
		if (keeper.getTactica().isKickGoal() || (  keeper.ballWithinRangeForIntercept() && 
				!keeper.getTactica().isOpponentWithinRadius(keeper.getBall().getPosition(), 400))) {
			keeper.getStateMachine().changeState(InterceptBall.instance);
			
			return;
		}
		
		if (keeper.canKick()) {
			keeper.getStateMachine().changeState(KickOff.instance);
			return;
		}


			if (keeper.isFarFromGoal()|| keeper.getTactica().isControlling()) {
				keeper.getStateMachine().changeState(ReturnToHome.instance);
				return;
			}  

		

	}

	public void exit(GoalKeeper keeper) {
		keeper.getSteering().seekOff();
		
	}

	public boolean onMessage(GoalKeeper keeper, Telegram telegram) {
		return false;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "TendGoal";
	}

}
